from django.shortcuts import render
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic import CreateView,UpdateView,DeleteView,ListView
from utils.mixins import AjaxResponseMixin
from django.urls import reverse_lazy
from link.models import Link


# Create your views here.

#添加链接类视图
class LinkCreateView(LoginRequiredMixin,AjaxResponseMixin,CreateView):
    login_url = reverse_lazy('user-login')
    model = Link
    fields = ['name','title','url','show_order']
    template_name_suffix = '_create_form'
    success_url = reverse_lazy('link-list')

    def get_context_data(self, **kwargs):
        context = super(LinkCreateView, self).get_context_data(**kwargs)
        context['active_page'] = 'link-add'
        return context


#修改链接视图
class LinkUpdateView(LoginRequiredMixin,AjaxResponseMixin,UpdateView):
    login_url = reverse_lazy("user-login")
    model = Link
    context_object_name = 'link'
    template_name_suffix = '_update_form'
    success_url = reverse_lazy('link-list')
    fields = ['name','title','url','show_order']

    def get_context_data(self, **kwargs):
        context = super(LinkUpdateView, self).get_context_data(**kwargs)
        context['active_page'] = 'link-update'
        return context


#删除链接视图
class LinkDeleteView(LoginRequiredMixin,AjaxResponseMixin,DeleteView):
    login_url = reverse_lazy('user-login')
    model = Link
    success_url = reverse_lazy('link-list')

    def get_context_data(self, **kwargs):
        context = super(LinkDeleteView, self).get_context_data(**kwargs)
        context['active_page'] = 'link-list'
        return context

#友情链接列表视图
class LinkListView(LoginRequiredMixin,ListView):
    login_url = reverse_lazy('user-login')
    model = Link
    context_object_name = 'link_list'

    def get_context_data(self, *, object_list=None, **kwargs):
        context = super(LinkListView, self).get_context_data(**kwargs)
        context['active_page'] = 'link-list'
        return context